<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path;
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
<title>教师请假申请-云校通-教师</title>
<style type="text/css">
	td{
		border:1px solid #d9d9d9 !important;
		text-align: center !important;
	}
	td input{
		text-align: center !important;
	}
	td:after{content: none !important;}
</style>
<jsp:include page="/WEB-INF/page/common/_header.jsp"></jsp:include>
<link rel="stylesheet" href="<%=basePath%>/style/weui.min.css">
<link rel="stylesheet" href="https://cdn.bootcss.com/jquery-weui/1.2.1/css/jquery-weui.min.css">
<script src="https://cdn.bootcss.com/jquery-weui/1.2.1/js/jquery-weui.min.js"></script>
<body ontouchstart id="tjjsqj">
	<div class="weui_tab tab-bottom">
		<div class="weui-header bg-blue"> 
	  		<div class="weui-header-left"> <a onclick="back()" href="javascript:" class="icon icon-109 f-white">取消</a> </div>
	    	<h1 class="weui-header-title">教师请假申请</h1>
	    </div>
		<div class="weui_tab_bd">
	        <div class="weui_cells"style="margin: 0;">
				<div class="weui_cell">
					<div class="weui_cell_hd">
						<label class="weui_label"><i class="icon icon-85 f20 f-blue"></i> 请假人</label>
					</div>
					<div class="weui_cell_bd weui_cell_primary">
						<input class="weui_input tr" value="${user.teacher.name }" readonly/>
					</div>
				</div>
				<div class="weui_cell weui_cell_select weui_select_after">
	                <div class="weui_cell_hd">
	                    <label class="weui_label"><i class="icon icon-67 f20 f-blue"></i> 请假类型</label>
	                </div>
	                <div class="weui_cell_bd weui_cell_primary weui_cell_ft">
	                	如：病假，事假等
	                </div>
	                <input id="leaveType" type="text" class="picker-input" value="" readonly/>
	            </div>
				<div class="weui_cell weui_cell_select">
					<div class="weui_cell_hd">
						<label class="weui_label"><i class="icon icon-51 f20 f-blue"></i> 开始时间</label>
					</div>
					<div class="weui_cell_bd weui_cell_primary">
						<input id="startTime" class="weui_input weui_input-select" onchange="getLeaveHours()" placeholder="请选择" readonly>
					</div>
				</div>
				<div class="weui_cell weui_cell_select">
					<div class="weui_cell_hd">
						<label class="weui_label"><i class="icon icon-51 f20 f-blue"></i> 结束时间</label>
					</div>
					<div class="weui_cell_bd weui_cell_primary">
						<input id="endTime" class="weui_input weui_input-select" onchange="getLeaveHours()" placeholder="请选择" readonly>
					</div>
				</div>
				<div class="weui_cell weui_cell_select leaveDays" >
	                <div class="weui_cell_hd">
	                    <label class="weui_label" style="width: 200px;"><i class="icon icon-67 f20 f-blue"></i> 请假时长（天）</label>
	                </div>
	                <div class="weui_cell_primary">
	                	<input id="leaveDays" class="weui_input tr" type="number" value="0.5" pattern="[0-9]*" readonly>
	                </div>
	            </div>
	            <div class="weui_cell weui_cell_select weui_select_after">
	            	<div class="weui_cell_hd">
	                    <label class="weui_label" style="width: 200px;"><i class="icon icon-67 f20 f-blue"></i> 是否需要调代课</label>
	                </div>
	                <div class="weui_cell_primary">
	                	<p style="display: inline-flex;">
	                		<label class="weui_cells_checkbox weui_check_label" for="is_lesson_yes">											
		                		<input type="radio" checked class="weui_check" value="1" name="isLesson" id="is_lesson_yes">				        					
		                		<i class="weui_icon_checked"></i></label>
		                	<label class="pl5">是</label>
	                	</p>
	                	<p style="display: inline-flex;padding-left: 20px;">
		                	<label class="weui_cells_checkbox weui_check_label" for="is_lesson_no">											
		                		<input type="radio" class="weui_check" value="0" name="isLesson" id="is_lesson_no">				        					
		                		<i class="weui_icon_checked"></i>										
		                	</label>
		                	<label class="pl5">否</label>
	                	</p>
	                </div>
	            </div>
				<div class="weui_cell weui_cell_select weui_select_after">
	                <div class="weui_cell_hd">
	                    <label class="weui_label"><i class="icon icon-67 f20 f-blue"></i> 请假事由</label>
	                </div>
	            </div>
	            <div class="weui_cell">
	                <div class="weui_cell_bd weui_cell_primary">
	                    <textarea id="leaveReason" class="weui_textarea" placeholder="请输入请假事由" rows="3"></textarea>
	                </div>
	            </div>
			</div>
	        <div class="weui_cells mt5">
	        	<div class="weui_cell xtbg-add-list-title">					
	        		<div class="weui_cell_bd weui_cell_primary">
	        			<p class="f16">请假期间工作安排</p>					
	        		</div>				
	        	</div>
	        	<div class="weui_cell bd-t0" style="padding:0;"> 	        	
					<table class="weui-table weui-border-tb">
	                	<thead>
			                <tr>
				                <th width="28%">学科</th>
				                <th width="20%">正课<br/>节数</th>
				                <th width="16%">自习<br/>节数</th>
				                <th width="16%">选修课<br/>节数</th>
				                <th width="20%">值日<br/>天数</th>
			                </tr>
		                </thead>
		                <tbody>
		                	<tr>
		                		<td>请假期间工作量</td>
		                		<td><input id="time1Lesson1" class="weui_input" placeholder="必填"/></td>
		                		<td><input id="time1Lesson2" class="weui_input" placeholder="必填"></td>
		                		<td><input id="time1Lesson3" class="weui_input" placeholder="必填"></td>
		                		<td><input id="time1Lesson4" class="weui_input" placeholder="必填"></td>
		                	</tr>
		                	<tr>
		                		<td>假前假后自己上课节（天）数</td>
		                		<td><input id="time2Lesson1" class="weui_input" placeholder="选填"/></td>
		                		<td><input id="time2Lesson2" class="weui_input" placeholder="选填"></td>
		                		<td><input id="time2Lesson3" class="weui_input" placeholder="选填"></td>
		                		<td><input id="time2Lesson4" class="weui_input" placeholder="选填"></td>
		                	</tr>
		                	<tr>
		                		<td>替班人及替班节（天）数</td>
		                		<td><input id="time3Lesson1" class="weui_input" placeholder="选填"/></td>
		                		<td><input id="time3Lesson2" class="weui_input" placeholder="选填"></td>
		                		<td><input id="time3Lesson3" class="weui_input" placeholder="选填"></td>
		                		<td><input id="time3Lesson4" class="weui_input" placeholder="选填"></td>
		                	</tr>
		                	<tr>
		                		<td>是否值日</td>
		                		<td><input id="isDuty" class="weui_input" placeholder="必填"></td>
		                		<td colspan="2">值日替班人</td>
		                		<td><input id="dutyReplacer" class="weui_input" placeholder="选填"></td>
		                	</tr>
		                	<tr>
		                		<td>是否晚修</td>
		                		<td><input id="isLate" class="weui_input" placeholder="必填"></td>
		                		<td colspan="2">晚修替班人</td>
		                		<td><input id="lateReplacer" class="weui_input" placeholder="选填"></td>
		                	</tr>
		                	<tr>
		                		<td>职&nbsp;&nbsp;&nbsp;&nbsp;务</td>
		                		<td><input id="position" class="weui_input" placeholder="必填"></td>
		                		<td colspan="2">职务委托人</td>
		                		<td><input id="positionConsigner" class="weui_input" placeholder="选填"></td>
		                	</tr>
		                </tbody>
	            	</table>
	            </div>
	        </div>
	        <div class="weui_cells weui_cells_access mt5">
				<div class="weui_cell">
					<div class="weui_cell_hd" style="margin-right: 5px;">
						<i class="icon icon-54 f18 f-blue"></i>
					</div>
					<div class="weui_cell_bd weui_cell_primary">
	                    <p> 图片</p>
	                </div>
					<div id="filePicker" class="weui_btn weui_btn_mini weui_btn_primary f-white bg-blue">上传</div>
				</div>
				<div class="weui_cell moments__post">
					<div class="weui_cell_bd">
						<div class="thumbnails weui_uploader_files">
							<ul id="img1" class="img-click">
							
							</ul>
			           </div>
			       </div>
			   </div>
			</div>
		   <div class="weui_cells weui_cells_access mt5">
				<div class="weui_cell" id="check">
					<div class="weui_cell_bd absolut">
	                    <p><i class="icon icon-99 f18 f-blue"></i> 审批人</p>
	                </div>
				</div>
				<div class="gzsp_wrap">
					<ul class="checkBtn"></ul>
					<div style="margin-left: 30px;" id="addBtn"><span class="add checkBtn"></span></div>
				</div>
		   </div>
	       <div class="weui_btn_area" id="leave-addBtn">
		        <a href="javascript:" class="weui_btn bg-blue">提交申请</a>
		    </div>
		    <div style="margin-bottom: 55px;"></div>
		</div>
	</div>
	
	<!--弹窗-->				
	<div class="weui_mask ueureiu weui_mask_visible" style="display: none;z-index:600">
	</div>
	<!--End弹窗-->
	
	<!-- 老师组织架构 start-->
	<div id="ls_div_box" class="weui_tab tab-bottom" style="display:none;z-index: 600;background-color: #fff;">
		<div class="weui_tab_bd">
	        <div class="weui-header bg-blue"> 
	        	<div class="weui-header-left" id="teacherBack"> <a class="icon icon-109 f-white">取消</a>  </div>
				<h1 class="weui-header-title">石门实验中学组织架构</h1>
			</div>
	        
	        <!--下拉选择-->
	        <div class="page-bd">  
		         <ul id="teacherList">
		     	 </ul>
		    </div>
	    	<script id="teacher-table-template" type="text/x-handlebars-template">	
 				{{#each list}}
					<li class="list1">
		                <div class="weui-flex">
		                   	<p class="weui-flex-item js-category"> {{groupName}}</p>
		                    <span class="js-category"><i class="icon icon-74"></i> 下级</span>
		                </div>
		                <div class="page-category js-categoryInner">
							<div class="weui_cells" style="margin: 0;">
								<ul class="list_3">
								{{#each subList}}
						            <li><div class="weui_cell_hd list_c3" style="display: inline-flex;padding-left: 10px;">
											<label class="weui_cells_checkbox weui_check_label" style="padding-right: 15px;" for="{{groupId}}_{{teacherId}}">
												<input type="checkbox" class="weui_check" value="{{teacherId}}" id="{{groupId}}_{{teacherId}}">
					        					<i class="weui_icon_checked"></i>
											</label>
											<label class="tea_name">{{teacherName}}</label>
										</div>
									</li>
								{{/each}}
								</ul>
							</div>
		                </div>
		            </li>
				{{/each}}
			</script>
	    	<!--End下拉选择-->
		</div>
	</div>
</body>
<jsp:include page="/WEB-INF/page/common/_footer.jsp"></jsp:include>
<script type="text/javascript">
$(function(){
   var nowDate = getTime(new Date(), 'yyyy-MM-dd');
   $("#startTime").datetimePicker({
        times: function () {
	        return [{values: ['上午', '下午']}];
	    },
		title:"选择开始时间",
		min: nowDate,
	    value: nowDate+" 上午"
   });
   $("#endTime").datetimePicker({
        times: function () {
	        return [{values: ['上午', '下午']}];
	    },
		title:"选择结束时间",
		min: nowDate,
	    value: nowDate+" 上午"
   });
   $("#leaveType").picker({
     title: "请选择请假类型",
     toolbarCloseText:'确定',
     onClose:function(){
     	$(this.input).closest(".weui_cell").find(".weui_cell_ft").text(this.cols[0].displayValue);
     },
     cols: [
       {
         textAlign: 'center',
         values: ["病假","事假","公差","婚假","产假","陪产假"],
         displayValues:["病假","事假","公差","婚假","产假","陪产假"],
       }
     ]
   });
   initApprover()
   $("#leave-addBtn").click(function(){
	   leaveSubmit();
   })
   //老师组织架构返回
   $("#teacherBack").click(function() {
		$('.ueureiu').hide();
		$("#ls_div_box").hide();
   });
   //老师组织架构弹出
   $("#addBtn").click(function() {
	   
 		//上下级拉动
		$("#teacherList .js-category").each(function(i, v) {
	   		v.removeEventListener("click", categoryClick, false);
	   		v.addEventListener('click', categoryClick);
	   	})
	   	var leaveDays = $("#leaveDays").val();
	 	if(leaveDays.length == 0) {
	 		$.alert("请先选择正确的请假时间","");
	 		return;
	 	}
	 	$('.ueureiu').show();
	 	$("#ls_div_box").show();
	 	$(".list_c3 input").change(function() {
			var obj = $(this);
			var otherChecked = obj.closest("li").siblings().find("input:checked");
			if(otherChecked.length > 0) {
				otherChecked[0].checked = false
			}
			var name = obj.closest(".list_c3").find(".tea_name").html();
			$(".gzsp_wrap>ul").html("");
			$(".gzsp_wrap>ul").append("<li><div class='gzsp'><span>"+name.substr(name.length-2,name.length)+"</span><i data-id='"+obj.val()+"'>"+name+"</i></div></li>");
			$("#teacherBack").click();
		});
 	})
	//拍照或从手机相册中选图接口
	$('#filePicker').on('click', function () {
	    wx.chooseImage({
	        count: 6,
	        needResult: 1,
	        sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
	        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
	        success: function (res) {
	       		var localIds = res.localIds; // 返回选定照片的本地ID列表，localId可以作为img标签的src属性显示图片
	       		wxuploadImage(localIds);
	        },
	        fail: function (res) {
	            alterShowMessage("操作提示", JSON.stringify(res), "1", "确定", "", "", "");
	        }

	    });
	});
})
function getLeaveHours(){
	var startTime = $("#startTime").val();
 	if(startTime.length == 0) {
 		return;
 	}
 	var endTime = $("#endTime").val();
 	if(endTime.length == 0) {
 		return;
 	}
 	var sd = startTime.split(" ");
 	var ed = endTime.split(" ");
 	if((sd[0] > ed[0]) || (sd[0] == ed[0] && sd[1] == "下午" && ed[1] == "上午")) {
 		$("#leaveDays").val(null);
 		$(".gzsp_wrap>ul").html('');
		$("#addBtn").attr("style","display: block;");
 		return;
 	}
 	var diffDay = new Date(ed[0]).getTime()-new Date(sd[0]).getTime();
 	if(diffDay >= 0) {
 		if(diffDay > 0) diffDay = diffDay/(24*60*60*1000);
 		if(sd[1] == "上午" && ed[1] == "上午") diffDay = parseFloat(diffDay) + 0.5;
 		else if(sd[1] == "上午" && ed[1] == "下午") diffDay = parseFloat(diffDay) + 1;
 		else if(sd[1] == "下午" && ed[1] == "下午") diffDay = parseFloat(diffDay) + 0.5;
 	}
 	$("#leaveDays").val(diffDay);
 	getApprover(diffDay);
}
var apploverData;
function initApprover() {
	$.ajax({
		url: basePath + '/ls/teacher/leave/approver',
		type: 'post',
		dataType: 'json',
		success: function(data) {
			if(data.code != '000') {
				$.alert(data.msg);
				return;
			}
			apploverData = data.data;
			getLeaveHours();
		}
	});
}
function getApprover(leaveDays) {
	var list = new Array();
	for(var i in apploverData) {
		if(leaveDays >= 3){
			if(apploverData[i].position.indexOf("1001") != -1){
				list.push(apploverData[i]);
			}
		} else if(leaveDays > 0.5 && leaveDays < 3) {
			if(apploverData[i].position.indexOf("3001") != -1){
				list.push(apploverData[i]);
			}
		} else {
			if(apploverData[i].position.indexOf("3003") != -1){
				list.push(apploverData[i]);
			}
		}
	}
	if(list.length == 1) {
		$("#addBtn").attr("style","display: none;");
		var name = list[0].name;
		$(".gzsp_wrap>ul").html("<li><div class='gzsp'><span>"+name.substr(name.length-2,name.length)+"</span><i data-id='"+list[0].id+"'>"+name+"</i></div></li>");
	}else {
		$(".gzsp_wrap>ul").html('');
		$("#addBtn").attr("style","display: block;");
		var html = '<div class="weui_cells" style="margin: 0;"><ul class="list_3">';
		for(var i in list) {
			html += '<li><div class="weui_cell_hd list_c3" style="display: inline-flex;padding-left: 10px;">\
							<label class="weui_cells_checkbox weui_check_label" style="padding-right: 15px;" for="tea_'+list[i].id+'">\
								<input type="checkbox" class="weui_check" value="'+list[i].id+'" id="tea_'+list[i].id+'">\
	        					<i class="weui_icon_checked"></i>\
							</label>\
							<label class="tea_name">'+list[i].name+'</label>\
						</div>\
					</li>';
		}
		html += '</ul></div>';
		$('#teacherList').html(html);
	}
}
var issubmit = false;
function leaveSubmit() {
	var leaveType = $("#leaveType").val();
	var startTime = $("#startTime").val();
	var endTime = $("#endTime").val();
	var leaveDays = $("#leaveDays").val();
	var leaveReason = $("#leaveReason").val();
	var isLesson = $("input[name='isLesson']:checked").val();
	var time1Lesson1 = $("#time1Lesson1").val();
	var time1Lesson2 = $("#time1Lesson2").val();
	var time1Lesson3 = $("#time1Lesson3").val();
	var time1Lesson4 = $("#time1Lesson4").val();
	var time2Lesson1 = $("#time2Lesson1").val();
	var time2Lesson2 = $("#time2Lesson2").val();
	var time2Lesson3 = $("#time2Lesson3").val();
	var time2Lesson4 = $("#time2Lesson4").val();
	var time3Lesson1 = $("#time3Lesson1").val();
	var time3Lesson2 = $("#time3Lesson2").val();
	var time3Lesson3 = $("#time3Lesson3").val();
	var time3Lesson4 = $("#time3Lesson4").val();
	var isDuty = $("#isDuty").val();
	var dutyReplacer = $("#dutyReplacer").val();
	var isLate = $("#isLate").val();
	var lateReplacer = $("#lateReplacer").val();
	var position = $("#position").val();
	var positionConsigner = $("#positionConsigner").val();
	if(leaveType.length == 0) {
		$.alert("请选择请假类型", "");
		return;
	}
	if(startTime.length == 0) {
		$.alert("请选择开始时间","");
		return;
	}
	if(endTime.length == 0) {
		$.alert("请选择结束时间","");
		return;
	}
	if(leaveDays.length ==0) {
		$.alert("请选择请假时长","");
		return;
	}
	if(parseFloat(leaveDays) < 0.5) {
		$.alert("请假时长最少0.5天","");
		return;
	}
	if(leaveReason.trim().length == 0) {
		$.alert("请输入请假原因");
		return;
	}
	if(time1Lesson1.trim().length == 0) {
		$.alert("请输入请假期间工作量(正课节数)");
		return;
	}
	if(time1Lesson2.trim().length == 0) {
		$.alert("请输入请假期间工作量(自习节数)");
		return;
	}
	if(time1Lesson3.trim().length == 0) {
		$.alert("请输入请假期间工作量(选修课节数)");
		return;
	}
	if(time1Lesson4.trim().length == 0) {
		$.alert("请输入请假期间工作量(值日节数)");
		return;
	}
	if(isDuty.trim().length == 0) {
		$.alert("请输入是否值日");
		return;
	}
	if(isLate.trim().length == 0) {
		$.alert("请输入是否晚修");
		return;
	}
	if(position.trim().length == 0) {
		$.alert("请输入职务");
		return;
	}
	var aObj = $($(".gzsp_wrap").find(".gzsp i"));
	var approverId = emptyToNull(aObj.attr("data-id"));
	if(approverId == null) {
		$.alert("请选择审批人", "");
		return;
	}
	var attachIds = new Array();
	$("#img1 .thumbnail").each(function(i, v) {
		attachIds.push($(v).attr("data-id"));
	});
	if(issubmit) return;
	issubmit = true;
	$.ajax({
   		url : basePath + "/ls/teacher/leave/create",
		type : "post",
		dataType : "json",
		data : {
			leaveType: leaveType,
			startTime: startTime,
			endTime: endTime,
			leaveDays: parseFloat(leaveDays),
			leaveReason: leaveReason,
			isLesson: isLesson,
			ids: attachIds.toString(),
			approver: aObj.html(),
			approverId: approverId,
			time1Lesson1: time1Lesson1,
		    time1Lesson2: time1Lesson2,
			time1Lesson3: time1Lesson3,
			time1Lesson4: time1Lesson4,
			time2Lesson1: time2Lesson1,
			time2Lesson2: time2Lesson2,
			time2Lesson3: time2Lesson3,
			time2Lesson4: time2Lesson4,
			time3Lesson1: time3Lesson1,
			time3Lesson2: time3Lesson2,
			time3Lesson3: time3Lesson3,
			time3Lesson4: time3Lesson4,
			isDuty: isDuty,
			dutyReplacer: dutyReplacer,
			isLate: isLate,
			lateReplacer: lateReplacer,
			position: position,
			positionConsigner: positionConsigner
		},
	    success: function(data){
	    	issubmit = false;
	    	if(data.code == '000') {
	    		$.toast("请假提交成功！");
	    		var url = basePath + "/ls/teacher/leave?v=" + new Date().getTime();
	    		setTimeout("javascript:location.href='"+url+"'", 1000);
	    	}else {
	    		$.toptips(data.msg);
	    	}
	    },
	    error: function(xhr, type){
	      
	    }
	 });
}

function categoryClick() {
	$parent = $(this).closest('li');
	if($parent.hasClass('js-show')){
        $parent.removeClass('js-show');
        $parent.find('i').removeClass('icon-35').addClass('icon-74');
    }else{
        $parent.siblings().removeClass('js-show');
        $parent.addClass('js-show');
        $parent.siblings().find('i').removeClass('icon-35').addClass('icon-74');
        $parent.find('i').removeClass('icon-74').addClass('icon-35');
    }
}
function wxuploadImage(localIds) {  
	var localId = localIds.pop();
    wx.uploadImage({  
        localId: localId, // 需要上传的图片的本地ID，由chooseImage接口获得  
        isShowProgressTips: 1, // 默认为1，显示进度提示  
        success: function (res) {  
            mediaId = res.serverId; // 返回图片的服务器端ID  
            wechatMediaDownload(mediaId);
            if(localIds.length > 0){
            	wxuploadImage(localIds);
            }
        },  
        fail: function (error) {  
            $.alert(Json.stringify(error));  
        }  
    });
}  
function wechatMediaDownload(mediaId) {
   	var params = {};
   	params.mediaId = mediaId;
    params.attachmentType = "image";
    params.relationType = "${relationType}";
    $.ajax({
 		url : basePath + "/ls/attchment/upload/wx",
 		type : "post",
 		data : params,
 		dataType : "json",
 		async : false,
 		success : function(data) {
 			if(data.code != '000') {
 				$.toast(data.msg, "cancel");
 			}else {
 				$('#img1').append('<li class="weui_uploader_file thumbnail" data-id="'+data.data.id+'" style="margin-right:0;margin-bottom:16px;position: relative;"><div class="img-del" onclick="imgDel(this)"><span class="icon icon-26"></span></div><img onclick="imgClick(this)" src="'+ imgUrl + data.data.attachmentUrl +'"/></li>'); 
 			}
 		}, error:function(){
 			$.alert("上传异常","");
  		}
 	});
}
</script>
</html>